'''
007 BOUNCE
Controls:       Tilt
Description:    Tilt left and right to have pig bounce on clouds
'''
import pygame
import spyral
import random  
import math


class game007(spyral.scene.Scene):
    def __init__(self, player):
        spyral.scene.Scene.__init__(self)
        self.root_camera = spyral.director.get_camera()
        self.camera = self.root_camera.make_child(layers = [0,1,2,3])
        self.group = spyral.sprite.Group(self.camera)
        
        self.player = player;
        self.timer = Timer.Timer(self.player.current_speed);
        
        self.scrollingbg1 = scrollingBG(1);
        self.scrollingbg2 = scrollingBG(2);
        self.pig = pig()
        self.clouds = [];
        self.makeClouds()
        
        self.bounced = False;
        self.counter = 0;
        
        self.group.add(self.timer, self.scrollingbg1, self.scrollingbg2, self.pig);
        
        
    def on_enter(self):
        bg = spyral.util.new_surface(Dic.geom['size'])
        self.camera.set_background(bg)
        

    def render(self):
        self.group.draw()
        self.root_camera.draw()
        
    def update(self, tick):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    spyral.director.pop()
                    exit(0)
                if event.key == pygame.K_r:
                    spyral.director.replace(game007(self.player))
                if event.key == pygame.K_a:
                    self.pig.setMovingLeft(True);
                if event.key == pygame.K_d:
                    self.pig.setMovingRight(True);
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a:
                    self.pig.setMovingLeft(False);
                if event.key == pygame.K_d:
                    self.pig.setMovingRight(False);
        
        #check if pig bounced
        if (pygame.sprite.collide_mask(self.pig, self.clouds[0])):
            self.pig.bounce();
            self.bounced = True;
        
        #if the pig just bounced then move the background and clouds
        #after a certain count delete lowest cloud
        if (self.pig.yvel <= 29 and -10 <= self.pig.yvel):
            for item in self.clouds:
                item.scroll(20)
            self.scrollingbg1.scroll(3)
            self.scrollingbg2.scroll(3)
            if (self.clouds[0].offscreen()):
                self.clouds.pop(0).kill();
        '''
        if (self.bounced == True):
            self.counter += 1;
            if (self.counter == 10):
                self.bounced = False;
                self.counter = 0;
                self.clouds.pop(0).kill()
            for item in self.clouds:
                item.scroll(self.counter * 6)
            self.scrollingbg1.scroll(self.counter * 2)
            self.scrollingbg2.scroll(self.counter * 2)
        '''
        #check if times up
        if (self.timer.timesUp()):
            if (self.winCondition() == False):
                self.player.decHealth();
            else:
                self.player.incCurrentGamesCompleted();
            spyral.director.pop()
        
        self.group.update();
    
    
    def winCondition(self):
        return self.pig.getWon();
    
    def makeClouds(self):
        self.clouds.append(cloud((490, 490)))
        self.group.add(self.clouds[0])
        for i in xrange(30):
            c = cloud( (random.randint(100, 900), (270 * (1 - i))) );
            self.clouds.append(c)
            self.group.add(c)
    
    
    

class scrollingBG(spyral.sprite.Sprite):
    def __init__(self, num):
        spyral.sprite.Sprite.__init__(self);
        if (num == 1):
            self.image = spyral.util.load_image(Dic.images['007scrollingbg1'])
            self.pos = (0,0)
        elif (num == 2):
            self.image = spyral.util.load_image(Dic.images['007scrollingbg2'])
            self.pos = (0, -640)
        self.layer = 0;
    
    def update(self, tick):
        if (Dic.geom['height'] <= self.pos[1]):
            yoffset = self.pos[1] - Dic.geom['height']
            self.pos = (self.pos[0], -640 + yoffset)
    
    def scroll(self, distance):
        self.pos = (self.pos[0], self.pos[1] + distance);
        

class pig(spyral.sprite.Sprite):
    def __init__(self):
        spyral.sprite.Sprite.__init__(self)
        self.img_1 = spyral.util.load_image(Dic.images['007pig1'])
        self.img_2 = spyral.util.load_image(Dic.images['007pig2'])
        self.img_3 = spyral.util.load_image(Dic.images['007pig3'])
        self.image = self.img_1
        self.rect.center = (490, 420)
        self.layer = 2;
        
        self.mask = pygame.mask.from_surface(self.image);
        self.yvel = 40;
        self.gravity = 3;
        
        self.won = True;
        self.bouncing = False;
        self.moving_left = False;
        self.moving_right = False;
    
    def update(self, tick):
        #vertical movement
        self.yvel -= self.gravity;
        self.rect = self.rect.move(0, -self.yvel);
        
        #horizontal movement
        if (self.moving_right == True):
            self.rect = self.rect.move(20,0);
        elif (self.moving_left == True):
            self.rect = self.rect.move(-20,0)
        
        #change images after bouncing    
        if (self.bouncing == True):
            self.bouncing = False;
        elif (self.image == self.img_2):
            self.image = self.img_3;
        elif (self.image == self.img_3):
            self.image = self.img_1;
        
        #losing condition   
        if (Dic.geom['height'] <= self.rect.top):
            self.won = False;
    
    def setMovingLeft(self, bool):
        self.moving_left = bool;
    
    def setMovingRight(self, bool):
        self.moving_right = bool;
        
    def bounce(self):
        self.yvel = 40;
        self.image = self.img_2;
        self.bouncing = True;
    
    def getWon(self):
        return self.won;


class cloud(spyral.sprite.Sprite):
    def __init__(self, pos):
        spyral.sprite.Sprite.__init__(self);
        self.image = spyral.util.load_image(Dic.images['007cloud'])
        self.rect.center = pos;
        self.layer = 1;
        
        self.mask = pygame.mask.from_surface(self.image);
    
    def scroll(self, distance):
        self.rect = self.rect.move(0, distance)
    
    def offscreen(self):
        if (Dic.geom['height'] <= self.rect.top):
            return True;
        else:
            return False;


if __name__ == "__main__":
    import sys
    sys.path.append("C:\Users\Patrick\paybackpigs\paybackpigs")
    from frenzy import Dic
    Dic.setImgFolder('../images/')
    from frenzy import Image
    from frenzy import Timer
    from frenzy import Player
    spyral.init()
    spyral.director.init(Dic.geom['size'], False, 'Payback Pigs', 30, 30)
    spyral.director.push(game007(Player.Player()))
    spyral.director.run()

else:
    import Image
    import Dic
    import Timer;